import { hasAuth } from "@/plugins/permission";
import { Directive, DirectiveBinding } from "vue";
import { useUserStore } from "@/store";

/**
 * 按钮权限
 */
export const hasPerm: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const userStore = useUserStore();
    // DOM绑定需要的按钮权限标识
    const { value } = binding;
    if (value) {
      console.log("userStore.user.routes", userStore.user.routes);

      if (!userStore.user.routes?.includes(value)) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error(
        "need perms! Like v-has-perm=\"['sys:user:add','sys:user:edit']\""
      );
    }
  },
};

/**
 * 角色权限
 */
export const hasRole: Directive = {
  // mounted(el: HTMLElement, binding: DirectiveBinding) {
  //   // DOM绑定需要的角色编码
  //   const { value: requiredRoles } = binding;
  //   if (requiredRoles) {
  //     if (!hasAuth(requiredRoles, "role")) {
  //       el.parentNode && el.parentNode.removeChild(el);
  //     }
  //   } else {
  //     throw new Error("need roles! Like v-has-role=\"['admin','test']\"");
  //   }
  // },
};
